package com.sitech.portal.system.action;

import org.apache.commons.beanutils.BeanUtils;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.RandomGUID;
import com.sitech.portal.system.domain.TblSYS_WORKGROUP;
import com.sitech.portal.system.form.WGrpFrom;
import com.sitech.portal.system.service.ISysWorkGroupService;

public class WorkGroupAction  extends JGridBaseAction{

	private ISysWorkGroupService sysWorkGroupService;
	private WGrpFrom theForm;
	private String flag;
	
	public WGrpFrom getTheForm() {
		return theForm;
	}
	public void setTheForm(WGrpFrom theForm) {
		this.theForm = theForm;
	}
	public ISysWorkGroupService getSysWorkGroupService() {
		return sysWorkGroupService;
	}
	public void setSysWorkGroupService(ISysWorkGroupService sysWorkGroupService) {
		this.sysWorkGroupService = sysWorkGroupService;
	}
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	
	public String loadWorkGroupManage(){
		return SUCCESS;
	}
	
	public String editWorkGroup() throws Exception{
			
		String id = theForm.getWORKGROUP_ID();
		if (id != null && id.length()>0) {
			TblSYS_WORKGROUP obj = sysWorkGroupService.findWorkGroupById(id);
			try {
				BeanUtils.copyProperties(theForm, obj);
			} catch (Exception be) {
				throw new Exception(be);
			}
		}
		return SUCCESS;
	}
	
	public String saveWorkGroup(){
		flag = "true";
		try {
			TblSYS_WORKGROUP obj = new TblSYS_WORKGROUP();
			BeanUtils.copyProperties(obj, theForm);
		
			if (theForm.getWORKGROUP_ID()!=null && theForm.getWORKGROUP_ID().length()>0) {
				sysWorkGroupService.updateWorkGroup(obj);
			} else {
				obj.setWORKGROUP_ID(RandomGUID.getRandomGUID());
				sysWorkGroupService.insertWorkGroup(obj);
				
			}
		} catch (Exception be) {
			flag = be.getMessage();
		}
		return SUCCESS;
	}
	/**
	 * 删除工作组并清空工作组下的人员
	 * @return
	 */
	public String deleteWorkGroup(){
		
		flag = "true";
		
		String id = theForm.getWORKGROUP_ID();

		try {
			sysWorkGroupService.deleteWorkGroup(id);
			
		} catch (Exception e) {
			flag = e.getMessage();
		}

		return SUCCESS;
	}
	/**
	 * 组内人员列表
	 * @return
	 * @throws Exception
	 */
	public String loadWorkGroupUserList() throws Exception{
		if(theForm!=null){
			String id = theForm.getWORKGROUP_ID();
			if (id != null && id.length()>0) {
				TblSYS_WORKGROUP obj = sysWorkGroupService.findWorkGroupById(id);
				try {
					BeanUtils.copyProperties(theForm, obj);
				} catch (Exception be) {
					be.printStackTrace();
				}
			}
		}
		return SUCCESS;
	}
	public String loadWorkGroupUserListQueryJson() {

		String id = theForm.getWORKGROUP_ID();

		IPage lst = sysWorkGroupService.finedUsersOfWorkGroup(id, this.getPageForm());

		this.setResult(lst);

		return SUCCESS;
	}
	public String addWorkGroupUser() throws Exception{
		flag = "true";
		
		String id = this.getRequest().getParameter("id");
		String workGroupId = theForm.getWORKGROUP_ID();

		try {
			String[] array = id.split(",");
			for(int i=0;i<array.length;i++){
				sysWorkGroupService.insertUserToWorkGroup(array[i], workGroupId);
			}
		} catch (Exception e) {
			flag = e.getMessage();
		}
		return SUCCESS;
	}
	public String removeWorkGroupUser() throws Exception{
		flag = "true";
		
		String id = this.getRequest().getParameter("id");
		String workGroupId = theForm.getWORKGROUP_ID();

		try {
			String[] array = id.split(",");
			for(int i=0;i<array.length;i++){
				sysWorkGroupService.deleteUserFromWorkGroup(array[i], workGroupId);
			}
			
		} catch (Exception e) {
			flag = e.getMessage();
		}
		return SUCCESS;
	}
}
